#include <header.h>

int main(){

    char *port = "8080";
    char *ip = "0.0.0.0";

    int socket_fd = socket(AF_INET, SOCK_STREAM, 0);


    struct sockaddr_in sock;
    sock.sin_family = AF_INET;
    sock.sin_port = htons(atoi(port));
    sock.sin_addr.s_addr = inet_addr(ip);
    bind(socket_fd, (struct sockaddr *)&sock, sizeof(sock));

    listen(socket_fd, 10);

    int net_fd= accept(socket_fd, NULL, NULL);

    size_t name_len = 0;
    recv(net_fd, &name_len, sizeof(size_t), 0);
    char buf_name[100] = {0};
    recv(net_fd, buf_name, name_len, 0);


    size_t len = 0;
    recv(net_fd, &len, sizeof(size_t), 0);
    char buf[4096] = {0};
    ssize_t ret = recv(net_fd, buf, len, 0);

    printf("file_name: %s \n", buf_name);
    printf("buf: %s \n", buf);

    int file_fd = open(buf_name, O_RDWR|O_CREAT, 0666);
    write(file_fd, buf, ret);
    close(file_fd);

    close(net_fd);
    close(socket_fd);

    return 0;
}

